package com.test.auth_db_auto.support.oauth2;

import com.test.auth_db_auto.model.OauthClientDetails;
import com.test.auth_db_auto.utils.CommonUtils;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.*;

/**
 * 自定义实现ClientDetails  不能使用lombok
 * @ Author: .Mr
 * @ ClassName BootClientDetails
 * @ Description TODO
 * @ date 2021/12/23 11:27
 * @ Version 1.0
 */
@Data
@SuppressWarnings("unchecked")
public class BootClientDetails implements ClientDetails {

    private OauthClientDetails client;

    public BootClientDetails(OauthClientDetails client) {
        this.client = client;
    }

    public BootClientDetails() {

    }

    @Override
    public String getClientId() {
        return client.getClientId();
    }

    @Override
    public Set<String> getResourceIds() {
        return client.getResourceId()!=null ?
                CommonUtils.transformStringToSet(client.getResourceId(), String.class) :  Collections.emptySet();
    }

    @Override
    public boolean isSecretRequired() {
        return client.getClientSecret()!=null;
    }

    @Override
    public String getClientSecret() {
        return client.getClientSecret();
    }

    @Override
    public boolean isScoped() {
        return this.getScope() != null && !this.getScope().isEmpty();
    }

    @Override
    public Set<String> getScope() {
        return client.getScope() !=null ?
                CommonUtils.transformStringToSet(client.getScope(),String.class) : Collections.emptySet();
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return client.getAuthorizedGrantTypes() !=null ?
                CommonUtils.transformStringToSet(client.getAuthorizedGrantTypes(),String.class) : Collections.emptySet();
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return client.getWebServerRedirectUri() !=null ?
                CommonUtils.transformStringToSet(client.getWebServerRedirectUri(),String.class) : Collections.emptySet();
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return (client.getAuthorities()!=null&&client.getAuthorities().trim().length()>0) ?
                AuthorityUtils.commaSeparatedStringToAuthorityList(client.getAuthorities()) : Collections.emptyList();
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return client.getAccessTokenValidity();
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return client.getRefreshTokenValidity();
    }

    @Override
    public boolean isAutoApprove(String scope) {
        if (this.getAutoApproveScopes() == null) {
            return false;
        } else {
            Iterator var2 = this.getAutoApproveScopes().iterator();
            String auto;
            do {
                if (!var2.hasNext()) {
                    return false;
                }
                auto = (String) var2.next();
            } while (!auto.equals("true") && !scope.matches(auto));
            return true;
        }
    }

    private Set<String> getAutoApproveScopes() {
        return client.getAutoApprove() != null ?
                CommonUtils.transformStringToSet(client.getAutoApprove(), String.class) :  Collections.emptySet();
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime
                * result
                + ((client.getAccessTokenValidity() == null) ? 0
                : client.getAccessTokenValidity());
        result = prime
                * result
                + ((client.getRefreshTokenValidity() == null) ? 0
                : client.getRefreshTokenValidity());
        result = prime * result
                + ((client.getAuthorities() == null) ? 0 : client.getAuthorities().hashCode());
        result = prime
                * result
                + ((client.getAuthorizedGrantTypes() == null) ? 0 : client.getAuthorizedGrantTypes()
                .hashCode());
        result = prime * result
                + ((client.getClientId() == null) ? 0 : client.getClientId().hashCode());
        result = prime * result
                + ((client.getClientSecret() == null) ? 0 : client.getClientSecret().hashCode());
        result = prime
                * result
                + ((client.getWebServerRedirectUri() == null) ? 0
                : client.getWebServerRedirectUri().hashCode());
        result = prime * result
                + ((client.getResourceId() == null) ? 0 : client.getResourceId().hashCode());
        result = prime * result + ((client.getScope() == null) ? 0 : client.getScope().hashCode());
        result = prime * result + ((client.getAdditionalInformation() == null) ? 0 : client.getAdditionalInformation().hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        OauthClientDetails other = (OauthClientDetails) obj;
        if (client.getAccessTokenValidity() == null) {
            if (other.getAccessTokenValidity() != null)
                return false;
        } else if (!client.getAccessTokenValidity().equals(other.getAccessTokenValidity()))
            return false;
        if (client.getRefreshTokenValidity() == null) {
            if (other.getRefreshTokenValidity() != null)
                return false;
        } else if (!client.getRefreshTokenValidity().equals(other.getRefreshTokenValidity()))
            return false;
        if (client.getAuthorities() == null) {
            if (other.getAuthorities() != null)
                return false;
        } else if (!client.getAuthorities().equals(other.getAuthorities()))
            return false;
        if (client.getAuthorizedGrantTypes() == null) {
            if (other.getAuthorizedGrantTypes() != null)
                return false;
        } else if (!client.getAuthorizedGrantTypes().equals(other.getAuthorizedGrantTypes()))
            return false;
        if (client.getClientId() == null) {
            if (other.getClientId() != null)
                return false;
        } else if (!client.getClientId().equals(other.getClientId()))
            return false;
        if (client.getClientSecret() == null) {
            if (other.getClientSecret() != null)
                return false;
        } else if (!client.getClientSecret().equals(other.getClientSecret()))
            return false;
        if (client.getWebServerRedirectUri() == null) {
            if (other.getWebServerRedirectUri() != null)
                return false;
        } else if (!client.getWebServerRedirectUri().equals(other.getWebServerRedirectUri()))
            return false;
        if (client.getResourceId() == null) {
            if (other.getResourceId() != null)
                return false;
        } else if (!client.getResourceId().equals(other.getResourceId()))
            return false;
        if (client.getScope() == null) {
            if (other.getScope() != null)
                return false;
        } else if (!client.getScope().equals(other.getScope()))
            return false;
        if (client.getAdditionalInformation() == null) {
            if (other.getAdditionalInformation() != null)
                return false;
        } else if (!client.getAdditionalInformation().equals(other.getAdditionalInformation()))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "AuthClientDetails [clientId=" + client.getClientId() + ", clientSecret="
                + client.getClientSecret() + ", scope=" + client.getScope() + ", resourceIds="
                + client.getResourceId() + ", authorizedGrantTypes="
                + client.getAuthorizedGrantTypes() + ", registeredRedirectUris="
                + client.getWebServerRedirectUri() + ", authorities=" + client.getAuthorities()
                + ", accessTokenValiditySeconds=" + client.getAccessTokenValidity()
                + ", refreshTokenValiditySeconds="
                + client.getRefreshTokenValidity() + ", additionalInformation="
                + client.getAdditionalInformation() + "]";
    }

}
